package com.apes.scm.rbac.model;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.audit.Auditing;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.tuple.date.DateTime;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 功能：用户（操作员）
 *
 * @author xul
 * @create 2018-03-15 16:00
 */
@Auditing
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = User.CONST_TABLE_NAME,indexes = {
		@Index(name = "IDX_AUT_USER_ORIGIN", columnList = "origin", unique = true)})
public class User extends Model {
	public static final String CONST_TABLE_NAME = "aut_users";

	@Id
	@Column(name="id")
	@ApesGenerator(code = CONST_TABLE_NAME, name = "用户")
	private Integer id;

	@Column(name="name", length = 30)
	private String name; //名称

	@Column(name="origin", length = 30)
	private String origin; //操作帐号

	@Column(name="password", length = 64)
	private String password; //密码

	@Column(name="disable", nullable = false, columnDefinition = "number(1,0) default 0")
	private boolean disable = false; //禁用标记

	@ManyToOne(fetch = FetchType.LAZY)
	@Name
	@JoinColumn(name = "person_id", referencedColumnName = "id")
	private PartyRole person; //人员
	
	@ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.LAZY)
	@JoinTable(name = "aut_post_users",
			joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
			inverseJoinColumns = {@JoinColumn(name = "post_id", referencedColumnName = "id")},
			indexes = {@Index(name = "idx_user_id", columnList = "user_id")}
	)
	private List<Post> posts = new ArrayList<>();

	// 定义操作员前端界面转化
	@Transient
	private List<JSONObject> postLits;

	@Column(name="access_id")
	@Comment("公钥")
	private String accessId; //公钥

	@Column(name="access_key")
	@Comment("私钥")
	private String accessKey; //私钥

	@JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "validity_time")
	@Comment("私钥有效期")
	private Date validityTime;// 私钥有效期

	@Column(name = "mobile_number", length = 11)
	@Comment("手机号")
	private String mobileNumber; //手机号


}
